import { constantRoutes } from '@/router'
import Layout from '@/layout' // Layout 是架构组件，不在后台返回，在文件里单独引入

export function getRouter(permission) {
    // console.log('luyou信息',permission);

    if (permission.children.length == 0) { //一级菜单Layout
        // let obj ={
        //     path: "/",
        //     component: Layout,
        //     redirect: permission.redirect,
        //     hidden: false,
        //     children: [{
        //         path: permission.path.slice(1),
        //         component: (resolve) => require(['@/views' + permission.component + '/index'], resolve),
        //         meta: permission.meta,
        //         name: permission.name
        //     }],
        // }
        // console.log('一级菜单Layout',obj);
        
        return {
            path: "/",
            component: Layout,
            redirect: permission.redirect,
            hidden: false,
            children: [{
                path: permission.path.slice(1),
                component: (resolve) => require(['@/views' + permission.component + '/index'], resolve),
                meta: permission.meta,
                name: permission.name
            }],
        }
    } else { //子菜单
        return {
            path: permission.path,
            component: (resolve) => require([`@/views/${permission.component}`], resolve),
            meta: permission.meta,
        }
    }
}

export function initRoute(permission) { //封装路由
    if (permission.children && permission.children.length > 0) { //如果存在子节点
        // console.log(permission.path);
        let route = {
            path: permission.path,
            redirect: permission.children[0].path,
            component: Layout,
            meta: permission.meta,
            children: []
        }
        permission.children.forEach(child => {  //递归封装子节点
            let routes = {
                path: child.path,
                component: (resolve) => require([`@/views${child.path}`], resolve),
                meta: child.meta,
                children: []
            }
            route.children.push(routes)
        })
        return route
    } else {  //不存在子节点直接返回
        return getRouter(permission)

    }
}

export function filterAsyncRoutes(routes) {
    const accessedRoutes = []
    routes.forEach(permission => {
        let routeNode = initRoute(permission)
        accessedRoutes.push(routeNode);  //push一个个封装好的路由数据
    })
    return accessedRoutes  //返回全部的路由数据
}

const state = {
    routes: [], //所有路由
    addRoutes: [],
    refreshPage: true
}

const mutations = {
    SET_ROUTES: (state, routes) => {
        state.addRoutes = routes
        state.routes = constantRoutes.concat(routes)    //将新路由数据和无权限数据合并
    },
    SET_REFRESHPAGE: (state) => {
        state.refreshPage = false
    }
}

const actions = {
    generateRoutes({ commit }, response) {
        return new Promise(resolve => {
            const accessedRoutes = filterAsyncRoutes(response) //全部的路由数据
            commit('SET_ROUTES', accessedRoutes)  //存入VueX
            commit('SET_REFRESHPAGE')
            resolve(accessedRoutes)  //返回用于addRoutes

        })
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}



















// import { asyncRoutes, constantRoutes } from '@/router'
// import Layout from '@/layout'
// /**
//  * 通过递归过滤异步路由表
//  * @param routes asyncRoutes
//  * @param roles
//  */

// const state = {
//     routes: [], //合并的routers
//     addRoutes: [] //res
// }
// export function filterAsyncRoutes(routers) {
//     const res = []
//     routers.forEach(router => {
//         const menu = {
//             path: router.path,
//             component: Layout,
//             redirect: router.redirect,
//             name: router.name,
//             meta: router.meta,
//         }
//         res.push(menu)
//     });
//     return res
// }


// const mutations = {
//     SET_ROUTES: (state, routes) => {
//         // 这个地方维护了两个状态一个是addRouters，一个是routes
//         state.addRoutes = routes
//         state.routes = constantRoutes.concat(routes)
//     }
// }

// const actions = {
//     generateRoutes({ commit }, asyncRoutes) { // roles == 1
//         return new Promise(resolve => {
//             const accessedRoutes = filterAsyncRoutes(asyncRoutes)
//             commit('SET_ROUTES', accessedRoutes)
//             resolve(accessedRoutes)
//         })
//     }
// }

// export default {
//     namespaced: true,
//     state,
//     mutations,
//     actions
// }

